package cn.com.greatwall.flowable.identity;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang3.ObjectUtils;
import org.flowable.idm.api.User;
import org.flowable.idm.engine.IdmEngineConfiguration;
import org.flowable.idm.engine.impl.UserQueryImpl;
import org.flowable.idm.engine.impl.persistence.entity.UserEntity;
import org.flowable.idm.engine.impl.persistence.entity.UserEntityImpl;
import org.flowable.idm.engine.impl.persistence.entity.UserEntityManagerImpl;
import org.flowable.idm.engine.impl.persistence.entity.data.UserDataManager;
import org.springframework.beans.factory.annotation.Autowired;

import cn.com.greatwall.system.modules.system.service.UserService;
import cn.com.greatwall.system.modules.system.service.dto.UserDto;
import cn.com.greatwall.system.modules.system.service.dto.criteria.UserQueryCriteria;

/**
 * @Author herw
 * @Time 2020-09-22 13:18:05
 * @Version 1.0
 * @Description: TODO(用一句话描述该文件做什么)
 */
public class CustomUserEntityManager extends UserEntityManagerImpl {
    @Autowired
    private UserService userService;
    
    public CustomUserEntityManager(IdmEngineConfiguration idmEngineConfiguration, UserDataManager userDataManager) {
        super(idmEngineConfiguration, userDataManager);
    }
    
    @Override
    public UserEntity findById(String entityId) {
        UserDto user = userService.findById(Long.parseLong(entityId));
        if (user == null) {
            return null;
        }
        UserEntity userEntity = new UserEntityImpl();
        userEntity.setId(user.getId().toString());
        userEntity.setFirstName(user.getUsername());
        userEntity.setLastName(user.getUsername());
        userEntity.setDisplayName(user.getNickName());
        userEntity.setEmail(user.getEmail());
        return userEntity;
    }
    
    @Override
    public List<User> findUserByQueryCriteria(UserQueryImpl query){
        UserQueryCriteria criteria = new UserQueryCriteria();
        if (ObjectUtils.isNotEmpty(query.getId())) {
            criteria.setId(Long.parseLong(query.getId()));
        }
        criteria.setBlurry(query.getFirstName());
        List<UserDto> sysUsers = userService.queryAll(criteria);
        if(sysUsers==null || sysUsers.size()==0) {
            return new ArrayList<>();
        }
        List<User> flowableUsers = new ArrayList<>();
        for (UserDto sysUser : sysUsers) {
            User user = new UserEntityImpl();
            user.setId(sysUser.getId().toString());
            user.setFirstName(sysUser.getUsername());
            user.setLastName(sysUser.getUsername());
            user.setDisplayName(sysUser.getUsername());
            user.setEmail(sysUser.getEmail());
            flowableUsers.add(user);
        }
        return flowableUsers;
    }
    
    @Override
    public long findUserCountByQueryCriteria(UserQueryImpl query) {
        UserQueryCriteria criteria = new UserQueryCriteria();
        if (ObjectUtils.isNotEmpty(query.getId())) {
            criteria.setId(Long.parseLong(query.getId()));
        }
        criteria.setBlurry(query.getFirstName());
        return userService.queryAll(criteria).size();
    }
}
